package com.zoweunion.mechanic.service.base.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import com.zoweunion.mechanic.util.constants.PushConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zoweunion.mechanic.dao.base.MessagePushDao;
import com.zoweunion.mechanic.plugins.jpush.JiguangPush;
import com.zoweunion.mechanic.plugins.websocket.WebSocketServer;
import com.zoweunion.mechanic.service.base.MessagePushService;
import com.zoweunion.mechanic.util.constants.Constants;
import com.zoweunion.mechanic.util.UuidUtil;

import cn.jpush.api.push.PushResult;
@Service
@Transactional
public class MessagePushServiceImpl implements MessagePushService{

	@Autowired
	private MessagePushDao messagePushDao; 
	@Autowired
	private WebSocketServer webSocketServer; 
	@Autowired
	private JiguangPush jiguangPush;
	
	@Transactional(readOnly=true)
	public List<Map<String, Object>> getPageQuery(Map<String, Object> reqMap) {
		return messagePushDao.pageQuery(reqMap);
	}

	public int insert(Map<String, Object> reqMap) {
		//如果已经存在一条记录 就不能再添加
		if(messagePushDao.pageQuery(reqMap)!=null && messagePushDao.pageQuery(reqMap).size()>0) {
			return 0;
		}
		reqMap.put("id", UuidUtil.get32UUID());
		return messagePushDao.insert(reqMap);
	}

	public int update(Map<String, Object> reqMap) {
		return messagePushDao.update(reqMap);
	}

	@Async
	@Override
	public void deMessagePush() {

		//.查询所有状态为2 3 4 的工单信息
		HashMap<String, Object> reqMap = new HashMap<String,Object>();
		int[]order_statuses={Constants.ORDER_STATUS_DSL,Constants.ORDER_STATUS_DTZ,Constants.ORDER_STATUS_YTZ};
		reqMap.put("order_statuses",order_statuses);
		List<Map<String,Object>>orderInfoList = messagePushDao.getOrderInfoByOrderStauts(reqMap);

		//遍历工单信息
		if(orderInfoList!=null && orderInfoList.size()>0) {
			for (Map<String, Object> orderInfo : orderInfoList) {
				//根据工单中的s_id  查询对应的消息推送信息
				Map<String, Object> messagePush = messagePushDao.getMessagePushByS_id(orderInfo);
				if(messagePush!=null) {
					//比较工单操作时间与当前时间,             
					Date handler_time = (Date) orderInfo.get("handle_time");
					Optional<Date> optional = Optional.ofNullable(handler_time);
					optional.ifPresent(handler_time1 -> {
					long time =System.currentTimeMillis()-handler_time1.getTime();
					//将订单状态码转为字符串
					String order_status="";
					if((int)(orderInfo.get("order_status"))==Constants.ORDER_STATUS_DSL) {
						order_status="待受理";
					}else if((int)(orderInfo.get("order_status"))==Constants.ORDER_STATUS_DTZ) {
						order_status="待听诊";
					}else if((int)(orderInfo.get("order_status"))==Constants.ORDER_STATUS_YTZ) {
						order_status="已听诊";
					}
					//拼接消息字符串
					StringBuilder message=new StringBuilder();
					message.append("订单编号：").append(orderInfo.get("order_no")).append(",订单状态：").append(order_status)
						.append(",已经等待：").append(time/60/1000).append("分钟");

					//当前订单等待时间超过三级推送时间
					if(time >= (int)messagePush.get("third_push_time")*60*1000) {
						try {
							webSocketServer.sendtoUser(message.toString(),messagePush.get("third_push_person").toString());
						} catch (Exception e) {

						}
						//补充移动端的消息推送
						try {

							PushResult pushResult = jiguangPush.jiguangPush(messagePush.get("third_push_person").toString(), PushConstants.KEY_ORDER_ID, message.toString(),orderInfo.get("order_id").toString());

						} catch (Exception e) {


						}

					}
					//当前订单等待时间超过二级推送时间
					else if(time >= (int)messagePush.get("second_push_time")*60*1000) {
						try {
							webSocketServer.sendtoUser(message.toString(),messagePush.get("second_push_person").toString());
						} catch (Exception e) {

						}
					}
					//当前订单等待时间超过一级推送时间
					else if(time >= (int)messagePush.get("first_push_time")*60*1000) {
						try {
							webSocketServer.sendtoUser(message.toString(),messagePush.get("first_push_person").toString());
						} catch (Exception e) {

						}
					}
				});

					}
			}

		}




	}


	
	
	
	
	

}
